<?php

namespace App;

use Composer\DependencyResolver\Request;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;


/**
 * App\Merchant
 *
 * @property int $id
 * @property int $logo_img_id 店铺logo图id
 * @property string $name 店铺名
 * @property string $desc 店铺简介
 * @property string $business_license 营业执照
 * @property int $business_license_img_id 营业执照的图片
 * @property string $province 所在省
 * @property string $city 所在市
 * @property string $area 所在区
 * @property string $address 详细地址
 * @property string $lng 经度
 * @property string $lat 纬度
 * @property string $business_time 营业时间
 * @property string $support_delivery_method 店铺支持的交货方式：存储json_encode后的数组
 * @property string|null $detail 详情
 * @property string $contact 联系方式
 * @property string $type 类型：待定
 * @property int $sort 排序，数值越小越靠前
 * @property int $status 营业状态：0未营业,1营业中，2店铺已关闭
 * @property int $review 审核状态：0未审核，1已通过，2已拒绝
 * @property \Illuminate\Support\Carbon|null $deleted_at
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @method static bool|null forceDelete()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Merchant newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Merchant newQuery()
 * @method static \Illuminate\Database\Query\Builder|\App\Merchant onlyTrashed()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Merchant query()
 * @method static bool|null restore()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Merchant whereAddress($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Merchant whereArea($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Merchant whereBusinessLicense($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Merchant whereBusinessLicenseImgId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Merchant whereBusinessTime($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Merchant whereCity($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Merchant whereContact($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Merchant whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Merchant whereDeletedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Merchant whereDesc($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Merchant whereDetail($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Merchant whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Merchant whereLat($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Merchant whereLng($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Merchant whereLogoImgId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Merchant whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Merchant whereProvince($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Merchant whereReview($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Merchant whereSort($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Merchant whereStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Merchant whereSupportDeliveryMethod($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Merchant whereType($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Merchant whereUpdatedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Merchant withTrashed()
 * @method static \Illuminate\Database\Query\Builder|\App\Merchant withoutTrashed()
 * @mixin \Eloquent
 * @property string $contact_person 联系人
 * @property string $company_name 企业名称
 * @property string $Unified_social_credit_code 统一社会信用代码
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Merchant whereCompanyName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Merchant whereContactPerson($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Merchant whereUnifiedSocialCreditCode($value)
 * @property int $enterprise_id 商家id
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Merchant whereEnterpriseId($value)
 * @property string|null $business_start_time 营业开始时间
 * @property string|null $business_end_time 营业结束时间
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Merchant whereBusinessEndTime($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Merchant whereBusinessStartTime($value)
 * @property int $bg_img_id 背景图id
 * @property string|null $bg_url 背景图url
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Merchant whereBgImgId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Merchant whereBgUrl($value)
 * @property int $classify_id 分类id
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Merchant whereClassifyId($value)
 */
class Merchant extends Model
{
    protected $table = 'merchant';
    use SoftDeletes;

    public static function getMerchantName($merchant_id){
        $data=self::whereId($merchant_id)->first();
        if ($data)
            return $data->name ?? '';
        return '';
    }
    public static function getDeliveryName($delivery_method){
        if ($delivery_method == 'express_delivery')
            return '配送地址';
        if ($delivery_method == 'self_pick')
            return '自提';
        if ($delivery_method == 'instant_delivery')
            return '骑手配送';
        return $delivery_method;
    }
    public static function getMerchantSupportDeliveryMethod($merchant_id){
        $data=self::whereId($merchant_id)->first();
        $result=[];
        if ($data){
            if ($data->support_delivery_method){
                $method_array=json_decode($data->support_delivery_method,true);
                if (count($method_array) > 0){
//                    $method_arr = [];
//                    if(in_array("instant_delivery",$method_array)){
//                        array_push($method_arr,"instant_delivery");
//                    }
//
//                    if(in_array("self_pick",$method_array)){
//                        array_push($method_arr,"self_pick");
//                    }
//
//                    if(in_array("express_delivery",$method_array)){
//                        array_push($method_arr,"express_delivery");
//                    }

                    foreach ($method_array as $item){
                        $temp=[
                            'code'=>$item,
                            'name'=>self::getDeliveryName($item)
                        ];
                        array_push($result,$temp);
                    }
                }
            }
        }
        return $result;
    }

    public static function getEnterpriseName($merchant_id){
        $data=self::whereId($merchant_id)->first();
        if ($data){
            return Enterprise::getEnterpriseName($data->enterprise_id);
        }
        return '';
    }
    public static function getEnterpriseId($merchant_id){
        $data=self::whereId($merchant_id)->first();
        return $data ? $data->enterprise_id : 0;
    }
    public static function getColName($col_name){
        if ($col_name == 'merchant_id') return '所属门店';
        if ($col_name == 'name') return '药品名称';
        if ($col_name == 'ssn') return '商品编号';
        if ($col_name == 'upc') return '商品条码';
        if ($col_name == 'selling_price') return '售价(￥元)';
        if ($col_name == 'stock') return '库存';
        if ($col_name == 'is_shop') return '展示状态';
        if ($col_name == 'drug_type') return '药品类型';//OTC、RX
        if ($col_name == 'goods_type') return '商品类型';//标准药品、保健食品等
        return $col_name;
    }
}
